/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

dojo.provide("nanosn.widgets.crud._ControlBar");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.form.Form");
dojo.require("dijit.Toolbar");
dojo.require("dijit.ToolbarSeparator");
dojo.require("dijit.form.Button"); 
dojo.declare("nanosn.widgets.crud._ControlBar", dijit._Widget,
{
    
    container: null,

    _buttonSave: null,
    _buttonCancel: null,
    _buttonDelete: null,
    _buttonNew: null,

    _CreateButton: function (label)
    {
        var tmpButton = new dijit.form.Button({
            id: this.id + "_button" + label,
            // note: should always specify a label, for accessibility reasons.
            // Just set showLabel=false if you don't want it to be displayed normally
            label: label,
            showLabel: true,
            iconClass: "dijitEditorIcon dijitEditorIcon" + label
        //onClick: eval("this.button" + label + "OnClick")
        });
        dojo.connect(tmpButton, "onClick", this, eval("this.button" + label + "OnClick"))
        return tmpButton;
    },


    postMixInProperties: function()
    {
        console.log("postMixInProperties");
    },

    _toolbar : null,
    buildRendering: function()
    {
        this._buttonSave = this._CreateButton("Save");
        this._buttonCancel = this._CreateButton("Cancel");
        this._buttonDelete = this._CreateButton("Delete");
        this._buttonNew = this._CreateButton("New");


        console.log("buildRendering _ControlBar");
        this._toolbar = new dijit.Toolbar({});

        dojo.forEach(["Save", "Delete", "New", "Cancel"], function(label){
            this._toolbar.addChild(eval("this._button" + label));
        }, this);
        
        this.domNode = this._toolbar.domNode;
        
    },

    buttonSaveOnClick: function(e)
    {
        console.log("Save Clicked");
        console.log(this.container.item);
        this.container._store.save();
        this.container._grid.sort();
        this.setCancelEditMode();
    },
    
    buttonDeleteOnClick: function(e)
    {
        console.log("Delete Clicked");
        if(this.container.item != null)
        {
            //TODO: dojo.alert("are u sure?");
            this.container._store.deleteItem(this.container.item);
            this.container._store.save();
            this.container.item = null;
        }
        //TODO: Should swich to next item or to table view
        this.setCancelEditMode();
    },

    /*boolean*/
    buttonNewClicked: false,
    buttonNewOnClick: function(e)
    {
        console.log("New Clicked");
        /*
         * 1. Make sure the editor tab is selected.
         * 2. Clear contents of everything.
         * 3. See if Store has NewItem function
         */
        //this.container._tabContainer.selectChild(this.container._contentPaneEditor);
        this.container.switchToEditorTab();
        this.container.item = this.container._store.newItem({});
        this.container.setItem(this.container.item);

        this.setNewMode();
        this.buttonNewClicked = true;

        dojo.byId("message").innerHTML = "Sweet Creating a new thing";

    },

    buttonCancelOnClick: function(e)
    {
        console.log("Cancel Clicked");
        this.container._store.revert();

        if(this.buttonNewClicked == true)
        {
            this.setCancelNewMode();
            //reset flag
            this.buttonNewClicked = false;
            this.container.switchToTableTab();
        }
        else
            this.setCancelEditMode();
    },

    setItem: function (item)
    {
        console.log("setItem");
        this.container.item = item;
        console.log(this.container.item);
        console.log(this);
    },

    postCreate: function()
    {
        this.setInitialMode();
    },

    setInitialMode: function()
    {
        this._buttonSave.attr("disabled",  true);
        this._buttonCancel.attr("disabled",  true);
        this._buttonDelete.attr("disabled",  true);
        this._buttonNew.attr("disabled",  false);
    },

    setNewMode: function()
    {
        this._buttonSave.attr("disabled",  false);
        this._buttonCancel.attr("disabled",  false);
        this._buttonDelete.attr("disabled",  true);
        this._buttonNew.attr("disabled",  true);
    },
    setCancelNewMode: function()
    {
        this._buttonSave.attr("disabled",  true);
        this._buttonCancel.attr("disabled",  true);
        this._buttonDelete.attr("disabled",  true);
        this._buttonNew.attr("disabled",  false);
    },
    
    setEditMode: function()
    {
        this._buttonSave.attr("disabled",  false);
        this._buttonCancel.attr("disabled",  false);
        this._buttonDelete.attr("disabled",  false);
        this._buttonNew.attr("disabled",  false);
    },
    setCancelEditMode: function()
    {
        this._buttonSave.attr("disabled",  true);
        this._buttonCancel.attr("disabled",  true);
        this._buttonDelete.attr("disabled",  false);
        this._buttonNew.attr("disabled",  false);
    }
});